package com.zgm.utils.ui;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zgm.utils.R;

/**
 * Created by 张公铭 on 2017/6/26 0026.
 */

public class MyDialog extends Dialog
{
    public MyDialog(Context context)
    {
        super(context);
    }

    public MyDialog(@NonNull Context context, @StyleRes int themeResId)
    {
        super(context, themeResId);
    }

    public static class Builder
    {
        private Context mContext;
        private boolean isShowPosibtn = false;
        private boolean isShowNetaibtn = false;

        public Builder(Context context)
        {
            mContext = context;
        }

        /**
         * 背景样式
         */
        private int root_style;
        /**
         * 标题
         */
        private String title;
        /**
         * 标题样式ID
         */
        private int titleStyleId;

        /**
         * 内容
         */
        private String message;
        private int msgStyleId;

        /**
         * 确认按钮 文本提示
         */
        private String positiveButtonText;
        private int posiStyleId;
        private DialogInterface.OnClickListener positiveOnClickListener;

        /**
         * 取消按钮 文本提示
         */
        private String negativeButtonText;
        private int negaStyleId;
        private DialogInterface.OnClickListener negativeOnclickListener;

        /**
         * 内容
         */
        private View contentView;

        public Builder setBackgourd(int root_style)
        {
            this.root_style = root_style;
            return this;
        }

        public Builder setTitle(String title, int styleId)
        {
            this.title = title;
            this.titleStyleId = styleId;
            return this;
        }

        public Builder setMessage(String message, int styleId)
        {
            this.message = message;
            this.msgStyleId = styleId;
            return this;
        }

        public Builder setPosiviteButton(String text, DialogInterface.OnClickListener listener, int styleId)
        {
            this.positiveButtonText = text;
            this.positiveOnClickListener = listener;
            this.posiStyleId = styleId;
            return this;
        }

        public Builder setNegativeButton(String text, DialogInterface.OnClickListener listener, int styleId)
        {
            this.negativeButtonText = text;
            this.negativeOnclickListener = listener;
            this.negaStyleId = styleId;
            return this;
        }

        public Builder setConetentView(View view)
        {
            this.contentView = view;
            return this;
        }

        public MyDialog create()
        {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final MyDialog dialog = new MyDialog(mContext, R.style.MyDialog);
            dialog.setCancelable(false);
            dialog.setCanceledOnTouchOutside(false);
            dialog.setCancelable(false);
            View view = inflater.inflate(R.layout.dailog, null);
            Window window = dialog.getWindow();
            window.setBackgroundDrawable(new BitmapDrawable());
            dialog.addContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            try
            {
                //硬件加速
                view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
            } catch (final NoSuchMethodError e)
            {
                Log.e("MyDialog", "error:" + e.getMessage());
            }

            LinearLayout ly_content_root = (LinearLayout) view.findViewById(R.id.ly_dialog_content);
            if (root_style > 0)
            {
                ly_content_root.setBackgroundResource(root_style);
            }

            TextView tv_title = (TextView) view.findViewById(R.id.tv_dialog_title);
            if (TextUtils.isEmpty(title))
            {
                tv_title.setVisibility(View.GONE);
            } else
            {
                tv_title.setText(title);
                if (titleStyleId > 0)
                {
                    tv_title.setBackgroundResource(titleStyleId);
                }
            }

            TextView tv_message = (TextView) view.findViewById(R.id.tv_dialog_message);
            if (TextUtils.isEmpty(message))
            {
                tv_message.setVisibility(View.GONE);
            } else
            {
                tv_message.setText(message);
                if (msgStyleId > 0)
                {
                    tv_message.setBackgroundResource(msgStyleId);
                }
            }

            TextView tv_positive = (TextView) view.findViewById(R.id.btn_dialog_positive);
            if (TextUtils.isEmpty(positiveButtonText))
            {
                tv_positive.setVisibility(View.GONE);
            } else
            {
                isShowPosibtn = true;
                tv_positive.setText(positiveButtonText);
                if (posiStyleId > 0)
                {
                    tv_positive.setBackgroundResource(posiStyleId);
                }

                if (positiveOnClickListener != null)
                {
                    tv_positive.setOnClickListener(new View.OnClickListener()
                    {
                        @Override
                        public void onClick(View v)
                        {
                            positiveOnClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                        }
                    });
                }

            }

            TextView tv_negative = (TextView) view.findViewById(R.id.btn_dialog_negative);
            if (TextUtils.isEmpty(negativeButtonText))
            {
                tv_negative.setVisibility(View.GONE);
            } else
            {
                isShowNetaibtn = true;
                tv_negative.setText(negativeButtonText);
                if (negaStyleId > 0)
                {
                    tv_negative.setBackgroundResource(negaStyleId);
                }
                if (negativeOnclickListener != null)
                {
                    tv_negative.setOnClickListener(new View.OnClickListener()
                    {
                        @Override
                        public void onClick(View v)
                        {
                            negativeOnclickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                        }
                    });
                }
            }

            if (isShowPosibtn && !isShowNetaibtn)
            {
                tv_positive.setBackgroundResource(R.drawable.text_solid_bottom_style);
            }

            LinearLayout ly_content = (LinearLayout) view.findViewById(R.id.ly_dialog_content);
            if (contentView != null)
            {
                ly_content.removeAllViews();
                ly_content.addView(contentView, new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            }

            dialog.setContentView(view);
            return dialog;
        }
    }
}
